<?php
function upload_zip($url,$payload,$cookie,$filepath,$filename){
$file = file_get_contents($filepath);
$multipart_boundary = "WebKitFormBoundaryxqBEt3680fbNgnaQ";
$params = "";
foreach($payload as $name=>$value){
	//echo $name.':'.$value.'<\br>';
	$params .= "--".$multipart_boundary."\r\n"
	. "Content-Disposition: form-data; name=\"".$name."\"\r\n\r\n"
	.$value
	."\r\n";
}
$params  = $params
    . "--".$multipart_boundary."\r\n"
    . "Content-Disposition: form-data; name=\"file[]\"; filename=\"".$filename."\"\r\n"
    . "Content-Type: application/octet-stream\r\n\r\n"
    . $file . "\r\n"
    . "--".$multipart_boundary."--\r\n";
//echo $params; 
//echo $ret[0];
//$first_newline = strpos($params, "\r\n");
//$multipart_boundary = substr($params, 2, $first_newline - 2);
//echo $multipart_boundary;
$headers = array();
$headers[] = 'Content-Length: ' . strlen($params);
$headers[] = 'Content-Type: multipart/form-data; boundary='.$multipart_boundary;
$headers[] = 'Cookie: '.$cookie;
// 1. 初始化
$ch = curl_init();
// 2. 设置选项，包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//echo http_build_query($data);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);


$output = curl_exec($ch);
//echo $output;
}
function get_jihuoma($url,$filepath){
  
    //copy($url, $filepath);
	$url_upload = 'https://natpacket.6te.net/ftp/index.php';
	$payload = array(
		"skin"=> "shinra",
			"language"=> "en",
			"protocol"=> "FTP",
			"ftpserver"=> "natpacket.6te.net",
			"ftpserverport"=> "",
			"username"=> "natpacket.6te.net",
			"password_encrypted"=> "32CA32570E2A70B7D776",
			"sshfingerprint"=> "",
			"ftpmode"=> "automatic",
			"passivemode"=> "no",
			"viewmode"=> "list",
			"sort"=> "",
			"sortorder"=> "",
			"consent_necessary"=> "",
			"consent_preferences"=> "",
			"consent_statistics"=> "",
			"consent_personalized_ads"=> "",
			"consent_nonpersonalized_ads"=> "",
			"user_email"=> "invalid_user_email",
			"privacy1"=> "0",
			"state"=> "upload",
			"state2"=> "",
			"screen"=> "2",
			//"directory"=>"/",
			//"file[]"=>file_get_contents('./udc.js'),
	);
	//$filepath = 'http://idea.medeming.com/a/jihuoma1.zip';
	//$filename = "jihuoma.zip";
	$cookie = "";
	upload_zip($url_upload,$payload,$cookie,$url,$filepath);
    $zip = new ZipArchive;

    $res = $zip->open($filepath);
    if ($res === TRUE) {
        $start = 0;
        $arr = array();
    
        $count = $zip->numFiles;
        while($count){
            $obj = array();
            $index = $count-1;
            $fileName =  $zip->getNameIndex($index);
            if(strpos($fileName,'.txt')){
                $obj['name'] = $fileName;
                $obj['value'] = stream_get_contents($zip->getStream($fileName));
                $arr[$start++] = $obj;
            }
            $count--;
        }

        $zip->close();

        echo json_encode($arr);
    } else {
        echo 'failed, code:' . $res;
    }
}
$url = 'http://idea.medeming.com/a/jihuoma1.zip';
$filepath= "zip.kmz";
header('Content-type: application/json');
get_jihuoma($url,$filepath);


